/**
 *                            License
 * THE WORK (AS DEFINED BELOW) IS PROVIDED UNDER THE TERMS OF THIS  
 * CREATIVE COMMONS PUBLIC LICENSE ("CCPL" OR "LICENSE"). 
 * THE WORK IS PROTECTED BY COPYRIGHT AND/OR OTHER APPLICABLE LAW.  
 * ANY USE OF THE WORK OTHER THAN AS AUTHORIZED UNDER THIS LICENSE OR  
 * COPYRIGHT LAW IS PROHIBITED.
 * 
 * BY EXERCISING ANY RIGHTS TO THE WORK PROVIDED HERE, YOU ACCEPT AND  
 * AGREE TO BE BOUND BY THE TERMS OF THIS LICENSE. TO THE EXTENT THIS LICENSE  
 * MAY BE CONSIDERED TO BE A CONTRACT,
 * THE LICENSOR GRANTS YOU THE RIGHTS CONTAINED 
 * HERE IN CONSIDERATION OF YOUR ACCEPTANCE OF SUCH TERMS AND CONDITIONS.
 * 
 */
package lineage.controller;

import java.sql.Timestamp;
import java.util.List;

import lineage.core.L1GameClient;
import lineage.database.tables.AccountTable;
import lineage.database.tables.bean.L1Account;
import lineage.util.collections.Lists;

/**
 * 账号控制器.
 * 
 * @version 2014年3月14日上午11:08:04
 * @author jrwz
 */
public final class L1AccountController {
    /** 账号在线列表. */
    private final List<String> onlineList = Lists.newArrayList();

    /**
     * 静态初始化器，由JVM来保证线程安全.
     */
    private static class Holder {
        static L1AccountController instance = new L1AccountController();
    }

    /**
     * 取得该类的实例.
     */
    public static L1AccountController getInstance() {
        return Holder.instance;
    }

    private L1AccountController() {
    }

    /**
     * 增加账号(数据库/总缓存).
     * 
     * @param client
     *            - 客户端对象
     * @param accountName
     *            - 账号名称
     * @param password
     *            - 密码
     * @return 如果增加成功则返回true
     */
    public boolean addAccount(final L1GameClient client, final String accountName, final String password) {
        // 将账号信息加入数据库与总缓存(缓存与数据库同步)
        return AccountTable.getInstance().addAccount(client, accountName, password);
    }

    /**
     * 删除账号(数据库/总缓存).
     * 
     * @param accountName
     *            - 账号名称
     * @return 如果删除成功则返回true
     */
    public boolean delAccount(final String accountName) {
        return AccountTable.getInstance().delAccount(accountName);
    }

    /**
     * 更改密码.
     * 
     * @param client
     *            - 客户端对象
     * @param password
     *            - 新密码
     * @return 如果修改成功则返回true
     */
    public boolean updatePassword(final L1GameClient client, final String password) {
        return AccountTable.getInstance().updatePassword(client, password);
    }

    /**
     * 更新最后登入日期.
     * 
     * @param client
     *            - 客户端对象
     * @param date
     *            - 要修改的日期(Timestamp对象)
     * @return 如果修改成功则返回true
     */
    public boolean updateLastDate(final L1GameClient client, final Timestamp date) {
        return AccountTable.getInstance().updateLastDate(client, date);
    }

    /**
     * 更新最后登入的ip地址.
     * 
     * @param client
     *            - 客户端对象
     * @return 如果修改成功则返回true
     */
    public boolean updateLastIP(final L1GameClient client) {
        return AccountTable.getInstance().updateLastIP(client);
    }

    /**
     * 检查是否已经有此账号.
     * 
     * @param accountName
     *            - 账号名称
     * @return 如果有则返回true
     */
    public boolean isAccount(final String accountName) {
        return AccountTable.getInstance().isAccountList(accountName);
    }

    /**
     * 检查密码是否正确.
     * 
     * @param accountName
     *            - 账号名称
     * @param password
     *            - 密码
     * @return 如果正确则返回true(如果没有找到账号也判断为false)
     */
    public boolean isPassword(final String accountName, final String password) {
        return AccountTable.getInstance().isLoginList(accountName, password);
    }

    /**
     * 增加在线账号(列表内).
     * 
     * @param accountName
     *            - 账号名称
     * @return 如果增加成功则返回true
     */
    public boolean add(final String accountName) {
        boolean flag = false;
        if (!onlineList.contains(accountName)) {
            onlineList.add(accountName);
            flag = true;
        }
        return flag;
    }

    /**
     * 删除在线账号(列表内).
     * 
     * @param accountName
     *            - 账号名称
     * @return 如果删除成功则返回true
     */
    public boolean remove(final String accountName) {
        boolean flag = false;
        if (onlineList.contains(accountName)) {
            onlineList.remove(accountName);
            flag = true;
        }
        return flag;
    }

    /**
     * 该账号是否在线(列表内).
     * 
     * @param accountName
     *            - 账号名称
     * @return 如果在线则返回true
     */
    public boolean isOnline(final String accountName) {
        boolean flag = false;
        if (onlineList.contains(accountName)) {
            flag = true;
        }
        return flag;
    }

    /**
     * 根据账号名取得L1Account对象(全部).
     * 
     * @param accountName
     *            - 账号名
     * @return L1Account对象(包括在线与不在线) 如果没有此账号则返回null
     */
    public L1Account getAccount(final String accountName) {
        return AccountTable.getInstance().getAccount(accountName);
    }

    /**
     * 根据账号名取得L1Account对象(在线).
     * 
     * @param accountName
     *            - 账号名
     * @return L1Account对象(只有在线) 如果没在线或没找到此账号则返回null
     */
    public L1Account getOnlineAccount(final String accountName) {
        L1Account account = null;
        if (onlineList.contains(accountName)) {
            account = AccountTable.getInstance().getAccount(accountName);
        }
        return account;
    }

    /**
     * 更新已创角色的总数量(数据库+缓存内).
     * 
     * @param client
     *            - 客户端对象
     * @param count
     *            - 要设置的数量
     * @return 如果修改成功则返回true
     */
    public boolean updateCharCount(final L1GameClient client, final int count) {
        return AccountTable.getInstance().updateCharCount(client, count);
    }

    /**
     * 根据帐号名取得该帐号下已创角色的总数量(缓存内).
     * 
     * @param accountName
     *            - 帐号名
     * @return 如果没找到或出错则返回0
     */
    public int getCharCount(final String accountName) {
        return AccountTable.getInstance().getCharCount(accountName);
    }

    /**
     * 取得在线账号数量.
     * 
     * @return 账号数量
     */
    public int getOnlineAccountCount() {
        return onlineList.size();
    }

    /**
     * 取得在线列表.
     * 
     * @return 列表
     */
    public List<String> getList() {
        return onlineList;
    }
}
